package com.smz.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smz.dao.CommodityDAO;
import com.smz.entity.Commodity;
import com.smz.service.CommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api
@RestController
public class CommodityController {
    @Autowired
    private CommodityService commodityService;

    @Autowired
    private CommodityDAO commodityDAO;

    @ApiOperation("查找所有商品")
    @GetMapping("/commodities")
    public ResponseEntity<List<Commodity>> findAllCommodity(){
        return ResponseEntity.ok(commodityService.findAllCommodity());
    }
    @ApiOperation("分页查询商品")
    @GetMapping("/pageCommodity/{current}/{rows}")
    public ResponseEntity<Page<Commodity>> findPageCommodity(@PathVariable("current") Integer current, @PathVariable("rows") Integer rows){
        Page<Commodity> commodity = commodityService.findPageCommodity(current, rows, null);
        return ResponseEntity.ok(commodity);
    }
    //dy
    @GetMapping("/commodities/{name}/{mid}")
    public ResponseEntity<List<Commodity>> findAllCommodity(@PathVariable("name") String name, @PathVariable Integer mid) {
        List<Commodity> list = commodityService.selectCommodity(name);
        List<Commodity> commodities=new ArrayList<Commodity>();
        for (Commodity c:list) {
            if(c.getTCargo().getTShelves().getTReservoir().getTMenu().getId()==mid){
                commodities.add(c);
            }
        }
        return ResponseEntity.ok(commodities);
    }

    @ApiOperation("增加商品")
    @PostMapping("/commodity")
    public ResponseEntity<String> addCommodity(@RequestBody Commodity commodity){
        commodityService.addCommodity(commodity);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/commodity/{id}")
    public ResponseEntity<String> removeCommodityById(@PathVariable("id") Integer id){
        commodityService.removeCommodityById(id);
        return ResponseEntity.ok("ok");
    }


    @ApiOperation("更新商品")
    @PutMapping("/commodity")
    public ResponseEntity<String> modifyCommodity(@RequestBody Commodity commodity){
        commodityService.modifyCommodity(commodity);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("根据商品名称模糊查询")
    @GetMapping("/commodity")
    public ResponseEntity<Page<Commodity>> selectCommodityByLike(@RequestParam("name") String name,@RequestParam("current") Integer current,@RequestParam("rows") Integer rows){
        Page<Commodity> pageCommodity = commodityService.findPageCommodityByWy(current, rows, name);
        return ResponseEntity.ok(pageCommodity);
    }

}
